package com.bjpowernode.config.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.config.context.ApplicationContextProvider;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TDicType;
import com.bjpowernode.entity.TDicValueDO;
import com.bjpowernode.service.DicTypeService;

import java.util.List;

public class StateConverter implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        int result = -1; //-1表示数据库不存在该"线索状态"值
        //拿到"线索状态"的中文名称：“未联系”、“已联系”
        String stateName = cellData.getStringValue();
        //把"线索状态"的中文名称 转成 对应的id值

        //拿到spring容器中的DicTypeService对象，然后调用里面的查询“线索状态”字典值的方法，拿到“线索状态”的字典数据
        DicTypeService dicTypeService = ApplicationContextProvider.getBean(DicTypeService.class);
        TDicType tDicType = dicTypeService.getDicTypeByTypeCode(Constants.CLUESTATE);
        //拿到称呼的List
        List<TDicValueDO> dicValueDOList = tDicType.getDicValueDOList();

        for (TDicValueDO tDicValueDO : dicValueDOList) {
            Integer id = tDicValueDO.getId();
            String typeValue = tDicValueDO.getTypeValue();
            if (stateName.equals(typeValue)) {
                result = id;
                break;
            }
        }
        return result;
    }
}
